import os
import sys
import pygame
import random
from pygame.locals import *

#initialize pygame
pygame.init()
#set width and height of window
width = 640
height = 480
#setup screen with width and height
screen = pygame.display.set_mode((width,height))
#game  caption
pygame.display.set_caption("p1x3l")

#variables for later use
x = width /2
y = height / 2

#set time delay between key responces
pygame.key.set_repeat(50, 50)

#load image/s to blit
materia = pygame.image.load("materia.gif")


clock = pygame.time.Clock()

#game loop
while True:

#preventing materia from going outside the outer bounds
    if x <= 0 or x >= width or y <= 0 or y >= height:
        print "Crash!"
        break

#key handling 
    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
                sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                sys.exit()
            if event.key == K_UP:
                y -= 2
            if event.key == K_DOWN:
                y += 2
            if event.key == K_LEFT:
                x -= 2
            if event.key == K_RIGHT:
                x += 2


    screen.fill((255,255,255))
    screen.blit(materia, (x,y))
    pygame.draw.rect(screen, (0,0,255),((50,50),(100,100)))
    pygame.display.flip()
    clock.tick(120)